package com.lcxw.hxzb.friendcircle.project.system.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcxw.hxzb.friendcircle.common.core.lang.UUID;
import com.lcxw.hxzb.friendcircle.common.utils.RedisCache;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.Response;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.FeignService.ImCommand;
import com.lcxw.hxzb.friendcircle.project.system.FeignService.TencentImRealization;
import com.lcxw.hxzb.friendcircle.project.system.domain.*;
import com.lcxw.hxzb.friendcircle.project.system.mapper.*;
import com.lcxw.hxzb.friendcircle.project.system.service.*;
import com.lcxw.hxzb.friendcircle.project.system.service.impl.YwpdGameServiceImpl;
import com.lcxw.hxzbcommon.common.RoomUtils;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.common.qiniu.Client;
import com.lcxw.hxzbcommon.common.qiniu.Hub;
import com.lcxw.hxzbcommon.common.qiniu.PiliException;
import com.lcxw.hxzbcommon.common.qiniu.RoomConfig;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 游戏订单Controller
 *
 * @author ruoyi
 * @date 2020-09-15
 */
@RestController
@Api(tags = "游戏订单Controller")
@RequestMapping("/client/order")
public class YwpdGameOrderController extends BaseController {
    @Autowired
    private IYwpdGameOrderService ywpdGameOrderService;
    @Autowired
    private IYwpdGameInfoService ywpdGameInfoService;
    @Autowired
    private IYwpdLabelService ywpdLabelService;
    @Autowired
    private IYwpdOrderLabelService ywpdOrderLabelService;
    @Autowired
    private TencentImRealization tencentImRealization;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private YwpdMemberMapper ywpdMemberMapper;
    @Resource
    YwpdConsumeMapper ywpdConsumeMapper;

    @Resource
    GiftSendMapper giftSendMapper;

    @Resource
    IncomeMapper incomeMapper;

    @Resource
    Proportion proportion;

    @Autowired
    private GiftService giftService;
    @Resource
    IncomeTotalMapper incomeTotalMapper;

    @Autowired
    private IYwpdGameService ywpdGameService;
    @PostMapping("/orderStatus")
    public AjaxResult orderStatus(@RequestBody YwpdGameOrder gameOrder){
        YwpdGameOrder order=ywpdGameOrderService.selectYwpdGameOrderById(gameOrder.getOrderId());
        return  AjaxResult.success(order);
    }



    /**
     * 新增游戏订单
     */
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody YwpdGameOrder ywpdGameOrder) {

        //判断当前陪玩者是否有正在进行中的游戏
        YwpdGameInfo ywpdGameInfo = ywpdGameInfoService.selectYwpdGameInfoById(ywpdGameOrder.getInfoId());
        List<YwpdGameOrder> sizes=ywpdGameOrderService.selectOrderLists(ywpdGameInfo.getUserId());
        if(sizes.size()>0){
            return AjaxResult.error("正在游戏中");
        }
        YwpdMember ywpdMember = ywpdMemberMapper.selectYwpdMemberByUserId(ywpdGameOrder.getUserId());
        BigDecimal earnBean = new BigDecimal(ywpdMember.getEarnBean());
        //判断用户金额是否够此次下单所需金额
        if (!(earnBean.compareTo(ywpdGameOrder.getTotal()) > -1)) {
            return AjaxResult.error("用户余额不够!");
        }
        //把用户金额减少
        ywpdMemberMapper.updateCoinByUserId(-ywpdGameOrder.getTotal().intValue(), ywpdGameOrder.getUserId());
        ywpdGameOrder.setStatus(2001L);
        int i = ywpdGameOrderService.insertYwpdGameOrder(ywpdGameOrder);
        Long orderId = ywpdGameOrder.getOrderId();
        String key = "gameOrder" + orderId;
        redisCache.setCacheObject(key, ywpdGameOrder, 10, TimeUnit.SECONDS);
        YwpdGame game=ywpdGameService.selectYwpdGameById(ywpdGameInfo.getGameId());
        String toUserId = String.valueOf(ywpdGameInfo.getUserId());
        Response msg = new Response();
        msg.setCode(ImCommand.OrderNotice);
        msg.setDesc(ywpdMember.getNickName()+"已发起游戏陪练邀请,"+game.getGameName()+"*"+ywpdGameOrder.getCount()+"局");
        Map map=new HashMap();
        long roomId = RoomUtils.generateShortUuid(8);
        long roomId1=RoomUtils.generateShortUuid(8);
        map.put("roomId",roomId);
        map.put("orderId",orderId);
        map.put("creatTime",StringUtil.getDateTime());
        //开始在七牛云创建直播流
        //初始化client
        Client cli = new Client(RoomConfig.ACCESS_KEY, RoomConfig.SECRET_KEY);
        //初始化Hub
        Hub hub = cli.newHub(RoomConfig.HUB);
        try {
            hub.create(roomId + "");
        } catch (PiliException e) {
            e.printStackTrace();
        }
        //设置房间的推拉流地址
        map.put("pullUrl",RoomConfig.getPullUrl(roomId));
        map.put("pushUrl",RoomConfig.getPushUrl(roomId));
        map.put("pullUrl1",RoomConfig.getPullUrl(roomId1));
        map.put("pushUrl1",RoomConfig.getPushUrl(roomId1));
        map.put("roomName","开黑语音");
        map.put("imgUrl",ywpdMember.getImgUrl());
        msg.setData(map);
        JSONObject imBody = tencentImRealization.generateImBody(JSON.toJSONString(msg), ywpdGameOrder.getUserId().toString(), toUserId);
        tencentImRealization.sendmsg(imBody);
        return AjaxResult.success(map);
    }

    /**
     * 查询游戏订单列表 拒绝的
     * 下单用户（付钱的）
     */
    @GetMapping("/orderList")
    public TableDataInfo orderList(YwpdGameOrder ywpdGameOrder) {
        startPage();
        List<YwpdGameOrder> gameOrderList = ywpdGameOrderService.orderList(ywpdGameOrder);
        return getDataTable(gameOrderList);
    }


    /**
     * 查询游戏订单列表 拒绝的
     * 下单用户（付钱的）
     */
    @GetMapping("/list/user/refuse")
    public TableDataInfo listUserRefuse(YwpdGameOrder ywpdGameOrder) {
        startPage();
        ywpdGameOrder.setStatus(1002L);
        List<YwpdGameOrder> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder);
        Map<Long, List<YwpdGameInfo>> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(null).stream().collect(Collectors.groupingBy(YwpdGameInfo::getInfoId));
        for (YwpdGameOrder gameOrder : gameOrderList) {
            Long orderId = gameOrder.getOrderId();
            YwpdGameInfo gameInfo = gameInfoList.get(orderId).get(0);
            gameOrder.setYwpdGameInfo(gameInfo);
        }
        return getDataTable(gameOrderList);
    }


    /**
     * 查询游戏订单列表 已完成
     * 下单用户（付钱的）
     */
    @PostMapping("/list/user")
    public TableDataInfo listUser(@RequestBody YwpdGameOrder ywpdGameOrder) throws Exception {
        startPage();
        List<YwpdGameOrder> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder);
        Map<Integer, List<Gift>> gifts = giftService.selectAllEnableGift().stream().collect(Collectors.groupingBy(Gift::getId));
        for(YwpdGameOrder order:gameOrderList){
            YwpdGameInfo gameInfo=ywpdGameInfoService.selectYwpdGameInfoById(order.getInfoId());
            if(null!=gameInfo){
                List<Gift> giftList = gifts.get(Integer.valueOf(gameInfo.getGiftId()+""));
                if(null!=giftList&&!giftList.isEmpty()){
                    order.setGiftIcon(giftList.get(0).getGiftIcon());
                }
                YwpdGame game=ywpdGameService.selectYwpdGameById(gameInfo.getGameId());
                order.setGameName(game.getGameName());
                YwpdMember ywpdMember = ywpdMemberMapper.selectYwpdMemberByUserId(gameInfo.getUserId());
                order.setMember(ywpdMember);
            }
        }
        return getDataTable(gameOrderList);
    }

    /**
     * 查询游戏订单列表 已完成
     * 下单用户（付钱的）
     */
    @PostMapping("/list/orderDetail")
    public AjaxResult orderDetail(@RequestBody YwpdGameOrder ywpdGameOrder) throws Exception {
       YwpdGameOrder order = ywpdGameOrderService.selectYwpdGameOrderById(ywpdGameOrder.getOrderId());
        Map<Integer, List<Gift>> gifts = giftService.selectAllEnableGift().stream().collect(Collectors.groupingBy(Gift::getId));
        if(null!=order){
            YwpdGameInfo gameInfo=ywpdGameInfoService.selectYwpdGameInfoById(order.getInfoId());
            if(null!=gameInfo){
                List<Gift> giftList = gifts.get(Integer.valueOf(gameInfo.getGiftId()+""));
                if(null!=giftList&&!giftList.isEmpty()){
                    order.setGiftIcon(giftList.get(0).getGiftIcon());
                }
                YwpdGame game=ywpdGameService.selectYwpdGameById(gameInfo.getGameId());
                order.setGameName(game.getGameName());
                YwpdMember ywpdMember = ywpdMemberMapper.selectYwpdMemberByUserId(gameInfo.getUserId());
                order.setMember(ywpdMember);
            }
        }
        return AjaxResult.success(order);
    }
    /**
     * 查询游戏订单列表 已完成
     * 下单用户（付钱的）
     */
    @PutMapping("/list/end")
    public AjaxResult end(@RequestBody YwpdGameOrder ywpdGameOrder) throws Exception {
        ywpdGameOrder.setStatus(3001l);
        int i=ywpdGameOrderService.updateYwpdGameOrder(ywpdGameOrder);
        return toAjax(i);
    }
    /**
     * 查询游戏订单列表 待接单
     * 下单用户（付钱的）
     */
    @GetMapping("/list/user/received")
    public TableDataInfo listUserReceived(YwpdGameOrder ywpdGameOrder) {
        startPage();
        ywpdGameOrder.setStatus(2001L);
        List<YwpdGameOrder> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder);
        Map<Long, List<YwpdGameInfo>> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(null).stream().collect(Collectors.groupingBy(YwpdGameInfo::getInfoId));
        for (YwpdGameOrder gameOrder : gameOrderList) {
            Long orderId = gameOrder.getOrderId();
            YwpdGameInfo gameInfo = gameInfoList.get(orderId).get(0);
            gameOrder.setYwpdGameInfo(gameInfo);
        }
        return getDataTable(gameOrderList);
    }

    /**
     * 查询游戏订单列表  已拒绝的
     * 陪玩的（收钱的）
     */
    @GetMapping("/list/accompany/refuse")
    public TableDataInfo listAccompanyRefuse(YwpdGameInfo ywpdGameInfo) {
        startPage();
        YwpdGameOrder ywpdGameOrder = new YwpdGameOrder();
        ywpdGameOrder.setStatus(1002L);
        //查order并根据infoId分组
        Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
        //查这个人的info
        List<YwpdGameInfo> gameInfos = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        List<YwpdGameOrder> orders = new LinkedList<>();
        //遍历info 拿到infoId
        for (YwpdGameInfo gameInfo : gameInfos) {
            Long infoId = gameInfo.getInfoId();
            //在order中分出order
            List<YwpdGameOrder> orderList = gameOrderList.get(infoId);
            if (orderList != null) {
                for (YwpdGameOrder gameOrder : orderList) {
                    gameOrder.setYwpdGameInfo(gameInfo);
                    orders.add(gameOrder);
                }
            }
        }
        return getDataTable(orders);
    }

    /**
     * 查询游戏订单列表  已完成的
     * 陪玩的（收钱的）
     */
    @GetMapping("/list/accompany")
    public TableDataInfo listAccompany(YwpdGameInfo ywpdGameInfo) {
        startPage();
        YwpdGameOrder ywpdGameOrder = new YwpdGameOrder();
        ywpdGameOrder.setStatus(3001L);
        //查order并根据infoId分组
        Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
        //查这个人的info
        List<YwpdGameInfo> gameInfos = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        List<YwpdGameOrder> orders = new LinkedList<>();
        //遍历info 拿到infoId
        for (YwpdGameInfo gameInfo : gameInfos) {
            Long infoId = gameInfo.getInfoId();
            //在order中分出order
            List<YwpdGameOrder> orderList = gameOrderList.get(infoId);
            if (orderList != null) {
                for (YwpdGameOrder gameOrder : orderList) {
                    gameOrder.setYwpdGameInfo(gameInfo);
                    orders.add(gameOrder);
                }
            }
        }
        return getDataTable(orders);
    }

    /**
     * 查询游戏订单列表  待接单
     * 陪玩的（收钱的）
     */
    @GetMapping("/list/accompany/received")
    public TableDataInfo listAccompanyReceived(YwpdGameInfo ywpdGameInfo) {
        startPage();
        YwpdGameOrder ywpdGameOrder = new YwpdGameOrder();
        ywpdGameOrder.setStatus(2001L);
        //查order并根据infoId分组
        Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(ywpdGameOrder).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
        //查这个人的info
        List<YwpdGameInfo> gameInfos = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        List<YwpdGameOrder> orders = new LinkedList<>();
        //遍历info 拿到infoId
        for (YwpdGameInfo gameInfo : gameInfos) {
            Long infoId = gameInfo.getInfoId();
            //在order中分出order
            List<YwpdGameOrder> orderList = gameOrderList.get(infoId);
            if (orderList != null) {
                for (YwpdGameOrder gameOrder : orderList) {
                    gameOrder.setYwpdGameInfo(gameInfo);
                    orders.add(gameOrder);
                }
            }
        }
        return getDataTable(orders);
    }

    /**
     * 获取游戏订单详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        //查订单
        YwpdGameOrder ywpdGameOrder = ywpdGameOrderService.selectYwpdGameOrderById(orderId);
        //查游戏信息
        YwpdGameInfo ywpdGameInfo = ywpdGameInfoService.selectYwpdGameInfoById(ywpdGameOrder.getInfoId());
        //查标签
        List<YwpdOrderLabel> orderLabelList = ywpdOrderLabelService.selectYwpdOrderLabelListById(ywpdGameOrder.getOrderId());
        List<YwpdLabel> ywpdLabels = ywpdLabelService.selectYwpdLabelList(null);
        Map<Long, List<YwpdLabel>> listMap = ywpdLabels.stream().collect(Collectors.groupingBy(YwpdLabel::getLabelId));
        for (int i = 0; i < orderLabelList.size(); i++) {
            Long labelId = orderLabelList.get(i).getLabelId();
            List<YwpdLabel> labels = listMap.get(labelId);
            orderLabelList.get(i).setYwpdLabel(labels.get(0));
        }
        ywpdGameOrder.setYwpdGameInfo(ywpdGameInfo);
        ywpdGameOrder.setYwpdOrderLabel(orderLabelList);
        return AjaxResult.success(ywpdGameOrder);
    }


    /**
     * 修改游戏订单 陪玩接单
     */
    @PutMapping("/receivingOrders")
    @Transactional
    public AjaxResult receivingOrders(@RequestBody YwpdGameOrder ywpdGameOrder) throws Exception {
        ywpdGameOrder.setStatus(1001L);
        Long orderId = ywpdGameOrder.getOrderId();
        redisCache.deleteObject("gameOrder" + orderId);
        String key = "gameStart" + orderId;
        YwpdGameOrder order = ywpdGameOrderService.selectYwpdGameOrderById(orderId);
        redisCache.setCacheObject(key, ywpdGameOrder, 20 * (order.getCount().intValue()), TimeUnit.MINUTES);
        YwpdGameInfo ywpdGameInfo = ywpdGameInfoService.selectYwpdGameInfoById(order.getInfoId());
        String toUserId = String.valueOf(ywpdGameInfo.getUserId());
        Response msg = new Response();
        msg.setCode(ImCommand.InvitationSuccessful);
        msg.setDesc("您的游戏陪练订单已邀请成功！");
        JSONObject imBody = tencentImRealization.generateImBody(JSON.toJSONString(msg), toUserId, order.getUserId().toString());
        tencentImRealization.sendmsg(imBody);
        //赠送礼物明细
        GiftSend giftSend = new GiftSend();
        giftSend.setRoomId(orderId);
        giftSend.setAddTime(StringUtil.getDateTime());
        giftSend.setUserId(ywpdGameInfo.getUserId());
        giftSend.setFromUserId(order.getUserId());
        giftSend.setGiftId(ywpdGameInfo.getGiftId().intValue());
        giftSend.setGiftNum(order.getCount().intValue());
        giftSend.setSource("mall");
        giftSendMapper.insertGiftEarn(giftSend);
        //将礼物赠送的金额加入到消费金额中
        YwpdConsume ywpdConsume = ywpdConsumeMapper.selectByUserId(order.getUserId());
        if (ywpdConsume == null) {
            YwpdConsume newConsume = new YwpdConsume();
            newConsume.setUserId(order.getUserId());
            newConsume.setConsume(order.getTotal().intValue());
            ywpdConsumeMapper.insert(newConsume);
        }
        ywpdConsumeMapper.updateConsumeByUserId(order.getTotal().intValue(), order.getUserId());
        //将该次收益记录到数据库中
        Income income = new Income();
        income.setUserId(ywpdGameInfo.getUserId());
        income.setFromUserId(order.getUserId());
        Double rmb = order.getTotal().doubleValue() / 10;
        income.setRmb(rmb);
        income.setType("礼物");
        income.setAddTime(StringUtil.getDateTime());
        incomeMapper.insert(income);
        double total = 0;
        double incomeToUnion = 0;
        double incomeEnable = 0;
        YwpdMember ywpdMember = ywpdMemberMapper.selectYwpdMemberByUserId(ywpdGameInfo.getUserId());
        //如果未签约，则该收入计入可提现收入
        if (ywpdMember.getSign() == 0) {
            incomeEnable = StringUtil.formatDouble(
                    rmb * proportion.getOrdinaryProp() * (1 - proportion.getOrdinaryTax()));
            total = StringUtil.formatDouble(rmb * proportion.getOrdinaryProp());//未签约用户，只包含60%的分成
        } else {
            //签约用户，总收入为分成后的收入，税前收入，税收将有公会开发票给平台报账
            total = StringUtil.formatDouble(rmb * proportion.getUnionProp());
            incomeToUnion = total;
        }
        QueryWrapper<IncomeTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", ywpdGameInfo.getUserId());
        IncomeTotal incomeTotal = incomeTotalMapper.selectOne(wrapper);
        if (null == incomeTotal) {
            IncomeTotal newIncomeTotal = new IncomeTotal();
            newIncomeTotal.setUserId(ywpdGameInfo.getUserId());
            newIncomeTotal
                    .setHistoryIncome(StringUtil.formatDouble(rmb * proportion.getOrdinaryProp()));
            newIncomeTotal.setIncomeTotal(total);
            newIncomeTotal.setIncomeEnable(incomeEnable);
            newIncomeTotal.setIncomeFish(0.00);
            newIncomeTotal.setIncomeToUnion(incomeToUnion);
            incomeTotalMapper.insert(newIncomeTotal);
        }else{
            incomeTotal
                    .setHistoryIncome(StringUtil
                            .formatDouble(incomeTotal.getHistoryIncome() + rmb * proportion.getOrdinaryProp()));
            incomeTotal.setIncomeTotal(
                    StringUtil.formatDouble(incomeTotal.getIncomeTotal() + total));
            incomeTotal
                    .setIncomeEnable(StringUtil.formatDouble(incomeTotal.getIncomeEnable() + incomeEnable));
            incomeTotal.setIncomeToUnion(
                    StringUtil.formatDouble(incomeTotal.getIncomeToUnion() + incomeToUnion));
            incomeTotalMapper.updateById(incomeTotal);
        }
        return toAjax(ywpdGameOrderService.updateYwpdGameOrder(ywpdGameOrder));
    }


    /**
     * 修改游戏订单 申请提前结束
     */
    @PutMapping("/applicationClosedEarly")
    public AjaxResult applicationClosedEarly(@RequestBody YwpdGameOrder ywpdGameOrder) {
        YwpdGameOrder order=ywpdGameOrderService.selectYwpdGameOrderById(ywpdGameOrder.getOrderId());
        order.setStatus(5001l);
        ywpdGameOrderService.updateYwpdGameOrder(order);
        Response msg = new Response();
        Map map=new HashMap();
        map.put("orderId",ywpdGameOrder.getOrderId());
        msg.setCode(ImCommand.EarlyEnd);
        msg.setDesc("申请提前结束！");
        msg.setData(map);
        JSONObject imBody = tencentImRealization.generateImBody(JSON.toJSONString(msg), ywpdGameOrder.getUserId()+"", order.getUserId()+"");
        tencentImRealization.sendmsg(imBody);
        return AjaxResult.success();
    }

    /**
     * 修改游戏订单 同意提前结束
     */
    @PutMapping("/earlyEnd")
    public AjaxResult earlyEnd(@RequestBody YwpdGameOrder ywpdGameOrder) {
        ywpdGameOrder.setStatus(3001L);
        Long orderId = ywpdGameOrder.getOrderId();
        redisCache.deleteObject("gameOrder" + orderId);
        String key = "gameStart" + orderId;
        redisCache.deleteObject(key);
        YwpdGameOrder order=ywpdGameOrderService.selectYwpdGameOrderById(ywpdGameOrder.getOrderId());
        YwpdGameInfo ywpdGameInfo = ywpdGameInfoService.selectYwpdGameInfoById(order.getInfoId());
        String toUserId = String.valueOf(ywpdGameInfo.getUserId());
        Response msg = new Response();
        msg.setCode(ImCommand.DoesTheUserAgree);
        msg.setDesc("同意提前结束！");
        JSONObject imBody = tencentImRealization.generateImBody(JSON.toJSONString(msg), ywpdGameOrder.getUserId().toString(), ywpdGameInfo.getUserId().toString());
        tencentImRealization.sendmsg(imBody);
        return toAjax(ywpdGameOrderService.updateYwpdGameOrder(ywpdGameOrder));
    }

    /**
     * 修改游戏订单 陪玩拒绝
     */
    @PutMapping("/refuse")
    public AjaxResult refuse(@RequestBody YwpdGameOrder ywpdGameOrder) {
        ywpdGameOrder.setStatus(1003L);
        redisCache.deleteObject("gameOrder" + ywpdGameOrder.getOrderId());
        return toAjax(ywpdGameOrderService.updateYwpdGameOrder(ywpdGameOrder));
    }

    /**
     * 修改游戏订单 评价订单
     */
    @PutMapping("/evaluate")
    @Transactional
    public AjaxResult evaluate(@RequestBody YwpdGameOrder ywpdGameOrder) {
        if(null!=ywpdGameOrder.getLabelIds()&&ywpdGameOrder.getLabelIds().length>0){
            for(Long labelId:ywpdGameOrder.getLabelIds()){
                YwpdOrderLabel ywpdOrderLabel=new YwpdOrderLabel();
                ywpdOrderLabel.setOrderId(ywpdGameOrder.getOrderId());
                ywpdOrderLabel.setLabelId(labelId);
                ywpdOrderLabelService.insertYwpdOrderLabel(ywpdOrderLabel);
            }
        }
        return toAjax(ywpdGameOrderService.updateYwpdGameOrder(ywpdGameOrder));
    }


    /**
     * 删除游戏订单
     */
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(ywpdGameOrderService.deleteYwpdGameOrderByIds(orderIds));
    }

    /**
     * 查询用户我的收益次数
     */
    @PostMapping("/myMoney")
    public AjaxResult myMoney(@RequestBody YwpdGameOrder ywpdGameOrder) {
        return AjaxResult.success(ywpdGameOrderService.selectMyMoney(ywpdGameOrder.getUserId()));
    }


    /**
     * 查询游戏订单列表 拒绝的
     * 下单用户（付钱的）
     */
    @GetMapping("/myMoney/detail")
    public TableDataInfo detail(YwpdGameOrder ywpdGameOrder) {
        startPage();
        ywpdGameOrder.setStatus(3001l);
        List<YwpdGameOrder> gameOrderList = ywpdGameOrderService.orderList(ywpdGameOrder);
        return getDataTable(gameOrderList);
    }


    /**
     * 我的收益明细
     */
    @GetMapping("/list/myMoneyDetail")
    public AjaxResult myMoneyDetail(@PathVariable("orderId") Long orderId) {
        //查订单
        YwpdGameOrder ywpdGameOrder = ywpdGameOrderService.selectYwpdGameOrderById(orderId);
        //查游戏信息
        YwpdGameInfo ywpdGameInfo = ywpdGameInfoService.selectYwpdGameInfoById(ywpdGameOrder.getInfoId());
        //查标签
        List<YwpdOrderLabel> orderLabelList = ywpdOrderLabelService.selectYwpdOrderLabelListById(ywpdGameOrder.getOrderId());
        List<YwpdLabel> ywpdLabels = ywpdLabelService.selectYwpdLabelList(null);
        Map<Long, List<YwpdLabel>> listMap = ywpdLabels.stream().collect(Collectors.groupingBy(YwpdLabel::getLabelId));
        for (int i = 0; i < orderLabelList.size(); i++) {
            Long labelId = orderLabelList.get(i).getLabelId();
            List<YwpdLabel> labels = listMap.get(labelId);
            orderLabelList.get(i).setYwpdLabel(labels.get(0));
        }
        ywpdGameOrder.setYwpdGameInfo(ywpdGameInfo);
        ywpdGameOrder.setYwpdOrderLabel(orderLabelList);
        return AjaxResult.success(ywpdGameOrder);
    }


}
